package com.example.demo.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.utils.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yolo
 * @since 2025-03-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private JwtUtil jwtUtil;

    @Override
    public String register(String username, String password) {
        String token = jwtUtil.generateToken(username);
        int userId = RandomUtil.randomInt();
        User user = new User();
        user.setPassword(new BCryptPasswordEncoder().encode(password));
        user.setUserId(userId);
        user.setUsername(username);
        if (save(user)) {
            return "注册成功！,token:" + token;
        }
        return "注册失败";
    }
}
